<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div id="user-tb">
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="goEditUser(0)">添加</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="goEditUser(1)">修改</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="deleteUser()">删除</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="assignMenu()">授权</a>
    <form>
        id:<input name="id" id="user-id">
        用户名：<input name="userName" id="user-name">
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-help',plain:true"
           onclick="searchUser()">查询</a>
    </form>
</div>
<table class="easyui-datagrid" id="user-table">
    <%--<thead>--%>
    <%--<tr>--%>
    <%--<th data-options="field:'id'">ID</th>--%>
    <%--<th data-options="field:'userName'">姓名</th>--%>
    <%--<th data-options="field:'password'">密码</th>--%>
    <%--</tr>--%>
    <%--</thead>--%>
</table>
<script type="text/javascript">
    $(function () {
        $("#user-table").datagrid({
            url: '/user.html?act=list',
            pagination: true,
            pageSize: 2,
            pageList: [2, 4, 6, 8, 10],
            singleSelect: true,
            toolbar: '#user-tb',
            selectOnCheck: false,
            checkOnSelect: false,
            columns: [[
                {field: 'id', title: 'ID'},
                {field: 'userName', title: '姓名'},
                {
                    field: 'password', title: '密码', formatter: function (value, row, index) {
                    return "<span style='color:#00ee00'>*****</span>";
                }
                },
                {field: 'ck', checkbox: true}
            ]]
        })
    });

    function deleteUser() {
        var rows = $("#user-table").datagrid("getChecked");
        var ids = new Array();
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
        }
        //alert(ids.join(","))
        $.ajax({
            url: "<%=request.getContextPath()%>/user.html?act=delete",
            data: {"ids": ids.join(",")},
            method: "post",
            success: function (result) {
                if (result.success) {
                    $("#user-table").datagrid('reload');
                } else {
                    alert(result.message)
                }
            }
        })
    }

    function searchUser() {
        $("#user-table").datagrid({
            "queryParams":
                {
                    "id":
                        $("#user-id").val(),
                    "userName":
                        $("#user-name").val()
                }
        })
    }

    function goEditUser(type) {
        var div = $("<div></div>")
        var id = "";
        if (type == 1) {
            var row = $("#user-table").datagrid("getSelected");
            if (row == null) {
                alert("请选择要编辑的数据");
                return;
            }
            id = row.id;
        }
        $(div).dialog({
            href: '<%=request.getContextPath()%>/user.html?act=go_edit&id=' + id,
            modal: true,
            width: 400,
            height: 300,
            buttons: [{
                text: '保存',
                iconCls: "icon-save",
                handler: function () {
                    $.ajax({
                        url: "<%=request.getContextPath()%>/user.html?act=edit",
                        data: $("#user-form").serialize(),
                        method: "post",
                        success: function (result) {
                            if (result.success) {
                                $("#user-table").datagrid('reload');
                                $(div).dialog("destroy");
                            } else {
                                alert(result.message)
                            }
                        }
                    })
                }
            }, {
                text: '取消',
                handler: function () {
                    $(div).dialog("destroy");
                }
            }],
            onClose: function () {
                $(div).dialog("destroy");
            }

        })
    }

    function assignMenu() {
        var div = $("<div></div>")
        var row = $("#user-table").datagrid("getSelected");
        if (row == null) {
            alert("请选择要编辑的数据");
            return;
        }
        var id = row.id;
        $(div).dialog({
            href: '<%=request.getContextPath()%>/user.html?act=go_assign_menu&userId=' + id,
            modal: true,
            width: 400,
            height: 300,
            buttons: [{
                text: '保存',
                iconCls: "icon-save",
                handler: function () {
                    var nodes = $('#user-menu-tree').tree('getChecked', ['checked', 'indeterminate']);
                    var menuIds = new Array();
                    for (var i = 0; i < nodes.length; i++) {
                        menuIds.push(nodes[i].id);
                    }
                    $.ajax({
                        url: "<%=request.getContextPath()%>/user.html?act=assign_menu",
                        data: "menuIds="+menuIds.join(",")+"&userId="+id,
                        method: "post",
                        success: function (result) {
                            if (result.success) {
                                $(div).dialog("destroy");
                            } else {
                                alert(result.message)
                            }
                        }
                    })
                }
            }, {
                text: '取消',
                handler: function () {
                    $(div).dialog("destroy");
                }
            }],
            onClose: function () {
                $(div).dialog("destroy");
            }

        })
    }
</script>